package com.aurora.supermarket.base;

import com.aurora.supermarket.bean.HttpResultBean;
import com.aurora.supermarket.proxy.HttpProxy;
import com.aurora.supermarket.util.Logger;
import com.aurora.supermarket.util.typebuilder.TypeBuilder;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;

import java.io.File;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <pre>
 * author: 王伟
 * time:2017/09/27
 * desc:
 * <pre>
 */

public class BaseModel implements IBaseModel {
    private RequestMethod method = RequestMethod.GET;
    private String tag;


    @Override
    public <T> void sendRequestToServerObject(String apiInterface, Map params, final CallBack<T> callBack, final Class<T> clazz) {
        switch (method){
            case GET:
                Map map = new HashMap();
                HttpProxy.getInstance().get(apiInterface, params == null ? map : params, new CallBack<String>() {
                    @Override
                    public void onSuccess(String s, String msg) {
                        Logger.e("tag",s);
                        Type type  = TypeBuilder
                                .newInstance(HttpResultBean.class)
                                .addTypeParam(clazz)
                                .build();
                        HttpResultBean<T> resultBean =  new Gson().fromJson(s,type);
                        if(resultBean.getSuccess()){
                            callBack.onSuccess(resultBean.getObj(),resultBean.getMsg());
                        }else{
                            callBack.onFail(resultBean.getMsg());
                        }
                    }

                    @Override
                    public void onFail(String msg) {
                        callBack.onFail(msg);
                    }
                });
                break;
            case POST:
                Map map2 = new HashMap();
                HttpProxy.getInstance().get(apiInterface, params == null ? map2 : params, new CallBack<String>() {
                    @Override
                    public void onSuccess(String s, String msg) {
                        Logger.e("tag",s);
                        Type type  = TypeBuilder
                                .newInstance(HttpResultBean.class)
                                .addTypeParam(clazz)
                                .build();
                        HttpResultBean<T> resultBean =  new Gson().fromJson(s,type);
                        if(resultBean.getSuccess()){
                            callBack.onSuccess(resultBean.getObj(),resultBean.getMsg());
                        }else{
                            callBack.onFail(resultBean.getMsg());
                        }
                    }

                    @Override
                    public void onFail(String msg) {
                        callBack.onFail(msg);
                    }
                });
                break;
        }
    }

    @Override
    public <T> void sendRequestToServerList(String apiInterface, Map params, final CallBack<List<T>> callBack, final Class<T> clazz) {
        switch (method){
            case GET:
                Map map = new HashMap();
                HttpProxy.getInstance().get(apiInterface, params == null ? map : params, new CallBack<String>() {
                    @Override
                    public void onSuccess(String s, String msg) {
                        Logger.e("tag",s);
                        Logger.e("tag",s);
                        Type type = TypeBuilder
                                .newInstance(HttpResultBean.class)
                                .beginSubType(List.class)
                                .addTypeParam(clazz)
                                .endSubType()
                                .build();
                        HttpResultBean<List<T>> resultBean =  new Gson().fromJson(s,type);
                        if(resultBean.getSuccess()){
                            callBack.onSuccess(resultBean.getObj(),resultBean.getMsg());
                        }else{
                            callBack.onFail(resultBean.getMsg());
                        }
                    }

                    @Override
                    public void onFail(String msg) {
                        callBack.onFail(msg);
                    }
                });
                break;
            case POST:
                Map map2 = new HashMap();
                HttpProxy.getInstance().post(apiInterface, params == null ? map2 : params, new CallBack<String>() {
                    @Override
                    public void onSuccess(String s, String msg) {
                        Logger.e("tag",s);
                        Logger.e("tag",s);
                        Type type = TypeBuilder
                                .newInstance(HttpResultBean.class)
                                .beginSubType(List.class)
                                .addTypeParam(clazz)
                                .endSubType()
                                .build();
                        HttpResultBean<List<T>> resultBean =  new Gson().fromJson(s,type);
                        if(resultBean.getSuccess()){
                            callBack.onSuccess(resultBean.getObj(),resultBean.getMsg());
                        }else{
                            callBack.onFail(resultBean.getMsg());
                        }
                    }

                    @Override
                    public void onFail(String msg) {
                        callBack.onFail(msg);
                    }
                });
                break;
        }
    }

    @Override
    public  void sendRequestToServer(final String apiInterface, Map params, final JsonCallback callBack) {
        switch (method){
            case GET:
                Map map = new HashMap();
                OkGo.get(apiInterface)
                        .params(params==null?map:params)
                        .tag(tag==null?"":tag)
                        .execute(callBack);
                break;
            case POST:
                Map map2 = new HashMap();
                OkGo.post(apiInterface)
                        .params(params==null?map2:params)
                        .tag(tag==null?"":tag)
                        .execute(callBack);
                break;
        }
    }


    @Override
    public  void sendPicToServer(String apiInterface, Map params, File file, final JsonCallback callBack) {
        OkGo.post(apiInterface)
                .params(params)
                .params("file",file)
                .execute(callBack);
    }

    @Override
    public void setMethod(RequestMethod method) {
        this.method = method;
    }

    @Override
    public void setTag(String tag) {
            this.tag = tag;
    }


    @Override
    public void cancelRequest(String tag) {
        if(tag==null){
            OkGo.getInstance().cancelAll();
        }else{
            OkGo.getInstance().cancelTag(tag);
        }
    }
}
